import {
  fileSuffix,
  interceptFileName
} from "../../../../utils/util";
import {
  uploadApi
} from "../../../apis/system";

// common/components/button/cameraButton/cameraButton.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    onUpload() {
      const self = this
      console.log('onUpload');
      wx.chooseMessageFile({
        count: 9,
        // image 只能拍摄图片或从相册选择图片
        // video 只能拍摄视频或从相册选择视频
        // mix 可同时选择图片和视频  
        success(res) {
          console.log(res, '=======');
          uploadApi.upload(res.tempFiles).then((rs) => {
            self.triggerEvent('onFileUpload', {
              value: rs.map(item => {
                return {
                  ...item,
                  previewName: interceptFileName(item.name),
                  previewUrl: `http://192.168.2.19:9001/jyw/${item.url}`,
                  suffix: fileSuffix(item.name)
                }
              })
            })
          })

        },
        fail(err) {
          console.log(err, 'err');
        }
      })
    },
  }
})